package com.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.base.ResultSetHandler;
import com.util.JdbcTemplate;
import com.vo.User;

public class UserDAOImpl implements UserDAO{
	private JdbcTemplate jdbcTemplate;
	public UserDAOImpl() {
		jdbcTemplate = new JdbcTemplate();
	}
	@Override
	public boolean save(User user) throws SQLException {
		boolean saveSuccessful=false;
		int i=0;
		String sql="insert into user(id,username,password)"
				+ "values(?,?,?)";
		System.out.println("userdao");
		i=jdbcTemplate.update(sql, user.getId(),user.getUsername(),user.getPassword());
		if(i==-1)
			saveSuccessful=false;
		return saveSuccessful;
	}

	@Override
	public int delete(int id) throws SQLException {
		return 0;
	}

	@Override
	public int update(User user) throws SQLException {
		
		return 0;
	}

	@Override
	public User findById(int id) throws SQLException {
		return null;
	}

	@Override
	public List<User> find() throws SQLException {
		
		return null;
	}

	@Override
	public boolean find(String username, String password) throws SQLException {
		String sql = "SELECT * FROM user WHERE username=? AND password=?";
		
		User user = (User)jdbcTemplate.query(sql, new ResultSetHandler() {
			public Object doHandler(ResultSet rs) throws SQLException {
				User user = null;
				if(rs.next()) {
					user = new User();
					user.setId(rs.getString(1));
					user.setUsername(rs.getString(2));
					user.setPassword(rs.getString(3));
				}
				return user;
			}
//
//			public Object doHandler1(PreparedStatement ps) throws SQLException {
//				return null;
//			}
		}, username, password);
		
		if(user != null) {
			return true;
		}
		return false;
	}
	

}
